package com.module.lemlin.http

import androidx.lifecycle.MutableLiveData

open class SubscribeViewModel<HttpData>(
    private val liveData: MutableLiveData<HttpResponseBody<HttpData>>
) : SubscribeListener<HttpData> {

    override fun onStart() {
        val status = (liveData.value ?: HttpResponseBody()).apply {
            status = Status.START
        }
        liveData.postValue(status)
    }

    override fun onSuccess(resp: HttpData) {
        val success = (liveData.value ?: HttpResponseBody()).apply {
            status = Status.SUCCESS
            data = resp
        }
        liveData.postValue(success)
    }

    override fun onFailure(throwable: Throwable) {
        val failure = (liveData.value ?: HttpResponseBody()).apply {
            status = Status.FAILURE.apply { this.desc = throwable.msg }
            error = throwable
        }
        liveData.postValue(failure)
    }

}